import {AudioPlayerManager} from "@/utils/audio.player.manager.ts";

export default function useSpeech(props:any) {
	const audioSts = ref(-1)//本地音频播放器用的状态 //-1未加载 0加载中 1已加载并停止 2播放中
	const speechText = ref('') //正在朗读的文本
	const callbackAudio = (event: string):any => {
		console.log('event = '+ event)
		switch (event) {
			case 'play': audioSts.value=2; break;
			case 'paused': audioSts.value=1; break;
			case 'ended': audioSts.value=1; break;
			case 'canplay': nextTick(()=>{playSpeak();}); break;
			case 'error': audioSts.value=1; nextTick(()=>{
				speechText.value='';
				AudioPlayerManager.getInstance().resetMsgContent('')
			}); break;
		}
	}

	/* methods */
	//语音播放内容
	const playSpeak = (content?:string)=>{
		// console.log('playSpeak',content)
		// console.log('speechText.value', speechText.value)
		audioSts.value = 0
		if(!content || speechText.value === content){
			AudioPlayerManager.getInstance().getAudioPlayer(speechText.value, callbackAudio).playAudio(props.source)
		}else{
			speechText.value = content
			AudioPlayerManager.getInstance().getAudioPlayer(content, callbackAudio).playAudio(props.source,content)
		}
	}
	//暂停播放语音
	const pauseSpeak = ()=>{
		AudioPlayerManager.getInstance().getAudioPlayer(speechText.value, callbackAudio).pauseAudio()
	}
	return {
		audioSts,speechText, //data
		playSpeak,pauseSpeak, //methods
	}
}